package edu.up.sky.controller;

import edu.up.sky.pojo.User;
import edu.up.sky.service.UserService;
import edu.up.sky.util.MailBean;
import edu.up.sky.util.MailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.math.BigInteger;

import static edu.up.sky.util.MailConstants.MAIL_REGISTER_ACTIVECODE_SUBJECT;

@Controller
@Slf4j
public class RegisterController {
    @Resource
    private UserService userService;

    @Resource
    private MailUtil mailUtil;

    @RequestMapping(value = {"/register"})
    public ModelAndView registerUser(ModelAndView mav, BigInteger uid, String classroom, String name, String password, String email, HttpServletRequest request) {
        User user = new User();
        user.setUid(uid);
        user.setName(name);
        user.setClassroom(classroom);
        user.setEmail(email);

        // TODO: 密码加密
        user.setPassword(password);

        // 账户信息存储在session
        HttpSession activeSession = request.getSession();
        activeSession.setAttribute("user_" + uid, user);
        activeSession.setMaxInactiveInterval(10 * 60);

        // 发送邮件
        sendActiveMail(uid, email, request);

        mav.setViewName("login");
        return mav;
    }

    @RequestMapping(value = {"/sendActiveMail"})
    public void sendActiveMail(BigInteger uid, String email,HttpServletRequest request) {
        log.info(email,"sendActiveMail Start");
        MailBean mailBean = new MailBean();
        mailBean.setRecipient(email);
        mailBean.setNickname(uid+"");
        mailBean.setSubject(MAIL_REGISTER_ACTIVECODE_SUBJECT);
        log.info(email,"email---");

        /*
         * 设置激活链接
         */
        // TODO 加密激活码
        String activeCode = "123456";
        // 激活链接
        String activeUrl = "localhost:8080/active?activeCode="+ activeCode + "&uid=" + uid;
        log.info("sendActiveUrl().activeUrl=>" + activeUrl);

        // session设置
        HttpSession activeSession = request.getSession();
        activeSession.setAttribute("activeCode_" + uid, activeCode);
        activeSession.setMaxInactiveInterval(10 * 60);
        log.info("sendActiveUrl().activeSession=>" + activeSession);

        mailBean.setContent("您在苍穹的激活链接为：" + activeUrl);

        // 发送激活邮件
        mailUtil.sendActiveHtmlMail(mailBean, activeUrl);
    }

    @RequestMapping(value = {"/active"})
    public ModelAndView activeUser(ModelAndView mav, String activeCode, Integer uid,HttpServletRequest request) {
        HttpSession activeSession = request.getSession();
        String aCode = (String) activeSession.getAttribute("activeCode_" + uid);
        log.info("activeUser().aCode=>" + aCode);

        if (activeCode.equals(aCode)) {
            // 插入数据库
            User user = (User) activeSession.getAttribute("user_" + uid);
            // 改为激活状态
//            user.setStatus(TableConstant.USER_STATUS_NORMAL);
            log.info("注册用户=>" + user);

            // 注册用户到user表
            userService.registerUser(user);


            mav.addObject("msg", "成功激活，快去登录吧！");
            mav.setViewName("Registration succeeded");

            // 销毁Session
            activeSession.removeAttribute("activeCode_" + uid);
        } else {
            mav.addObject("msg", "激活失败，请确认链接正确！");
            mav.setViewName("active_page");
        }

        return mav;
    }

}
